package com.drinks.yiniang.ui.activity.user.setting;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.drinks.yiniang.R;
import com.drinks.yiniang.common.time.TimeCount;
import com.drinks.yiniang.entity.common.StringJson;
import com.drinks.yiniang.io.api.CommonApiIO;
import com.drinks.yiniang.ui.activity.user.login.VerificationCodeActivity;
import com.drinks.yiniang.utils.ToastUtils;
import com.hansen.library.Constants;
import com.hansen.library.entity.BaseJson;
import com.hansen.library.io.tuple.Tuple2;
import com.hansen.library.listener.APIRequestCallback;
import com.hansen.library.ui.widget.layout.TextEditLayout;
import com.hansen.library.ui.widget.textview.BorderTextView;
import com.hansen.library.ui.activity.BaseTranBarActivity;
import com.hansen.library.listener.OnNavgationBarClickListener;
import com.hansen.library.ui.widget.nav.NavigationBarLayout;
import com.hansen.library.utils.StringUtils;

/**
 * 忘记支付密码页面
 *
 * @author han
 * @date 2019/09/08
 */
public class ForgotPaymentPasswordActivity extends BaseTranBarActivity implements OnNavgationBarClickListener,
        TimeCount.MyCountDownListener {

    //nav
    private NavigationBarLayout nav_bar_forgot_payment_pwd;
    //手机号
    private TextEditLayout tel_forgot_payment_pwd_phone;
    //错误提示信息
    private TextView tv_forgot_payment_pwd_error_tips;
    //验证码
    private EditText et_forgot_payment_pwd_code;
    //获取验证码
    private BorderTextView tv_forgot_payment_pwd_get_code;
    //确认
    private Button btn_forgot_payment_pwd_sure;

    //倒计时
    private TimeCount mTimeCount;

    @Override
    protected void initView() {
        nav_bar_forgot_payment_pwd = findViewById(R.id.nav_bar_forgot_payment_pwd);

        tel_forgot_payment_pwd_phone = findViewById(R.id.tel_forgot_payment_pwd_phone);

        tv_forgot_payment_pwd_error_tips = findViewById(R.id.tv_forgot_payment_pwd_error_tips);
        tv_forgot_payment_pwd_get_code = findViewById(R.id.tv_forgot_payment_pwd_get_code);

        et_forgot_payment_pwd_code = findViewById(R.id.et_forgot_payment_pwd_code);

        btn_forgot_payment_pwd_sure = findViewById(R.id.btn_forgot_payment_pwd_sure);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        mContext = this;

        mTimeCount = new TimeCount(60000, 1000);//构造CountDownTimer对象
    }

    @Override
    protected void initListener() {
        nav_bar_forgot_payment_pwd.setOnNavgationBarClickListener(this);
        tv_forgot_payment_pwd_get_code.setOnClickListener(this);
        btn_forgot_payment_pwd_sure.setOnClickListener(this);

        mTimeCount.setCountDownListener(this);
    }

    @Override
    public void widgetClick(View v) {
        switch (v.getId()) {
            case R.id.tv_forgot_payment_pwd_get_code://获取验证码
                getVerificationCode();
                break;
            case R.id.btn_forgot_payment_pwd_sure://确认
                doPhoneCodeVerify();
                break;
            default:
                break;
        }
    }

    @Override
    public int getTranBarResID() {
        return R.layout.activity_forgot_payment_password;
    }

    @Override
    public void onBackClick(View v) {
        finish();
    }

    @Override
    public void onEditClick(View v) {

    }

    @Override
    public void onTimerTick(long millisUntilFinished) {
        tv_forgot_payment_pwd_get_code.setEnabled(false);
        tv_forgot_payment_pwd_get_code.setText(getString(R.string.text_revalidation_seconds, String.valueOf(millisUntilFinished / 1000)));
    }

    @Override
    public void onTimerFinish() {
        tv_forgot_payment_pwd_get_code.setText(getString(R.string.text_revalidation));
        tv_forgot_payment_pwd_get_code.setEnabled(true);
    }

    //验证手机验证码
    private void doPhoneCodeVerify() {
        String mobile = tel_forgot_payment_pwd_phone.getEditText();

        if (StringUtils.isEmpty(mobile)) {
            ToastUtils.showShort(R.string.text_hint_input_phone_num);
            return;
        }

        if (!StringUtils.isPhone(mobile)) {
            ToastUtils.showShort(R.string.text_hint_input_true_phone_num);
            return;
        }

        String code = StringUtils.getEditTextString(et_forgot_payment_pwd_code.getText());

        if (StringUtils.isEmpty(code)) {
            ToastUtils.showShort(R.string.text_hint_input_verification_code);
            return;
        }

        showLoadingDialog();

        CommonApiIO.getInstance().doPhoneCodeVerify(mobile, code, new APIRequestCallback<StringJson, Tuple2<Integer, String>>() {
            @Override
            public void onSuccess(StringJson res) {
                setResult(Constants.RESULT_CODE_NORMAL);
                finish();
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                ToastUtils.showShort(error._2);
            }

            @Override
            public void onComplete() {
                dismissDialog();
            }
        });
    }

    private void getVerificationCode() {
        String mobile = tel_forgot_payment_pwd_phone.getEditText();

        if (StringUtils.isEmpty(mobile)) {
            ToastUtils.showShort(getString(R.string.text_hint_input_phone_num));
            return;
        }

        if (!StringUtils.isPhone(mobile)) {
            ToastUtils.showShort(getString(R.string.text_hint_input_true_phone_num));
            return;
        }

        showLoadingDialog();

        CommonApiIO.getInstance().getSmsCode(mobile, new APIRequestCallback<BaseJson, Tuple2<Integer, String>>() {
            @Override
            public void onSuccess(BaseJson res) {
                mTimeCount.start();//计时开始
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                ToastUtils.showShort(error._2);
            }

            @Override
            public void onComplete() {
                dismissDialog();
            }
        });
    }

    @Override
    protected void onDestroy() {
        if (mTimeCount != null) {
            mTimeCount.setCountDownListener(null);
            mTimeCount.cancel();
            mTimeCount = null;
        }
        super.onDestroy();
    }
}
